//
//  BSTopic.m
//  01-BuDeJie
//
//  Created by 1 on 15/12/23.
//  Copyright © 2015年 xiaomage. All rights reserved.
//

#import "BSTopic.h"

@implementation BSTopic

//这是一个方法名,需要返回一个值,这里是返回成员变量
-(NSString *)created_at{
    //判断年
    //判断月
    //判断天
    //判断小时
    //判断分针
    
    // 帖子的审核通过时间
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *createdAtDate = [fmt dateFromString:_created_at];
    
    if (createdAtDate.bs_isThisYear) { // 今年
        
        if (createdAtDate.bs_isYesterday) { // 昨天
            fmt.dateFormat = @"昨天 HH:mm:ss";
            
            return [fmt stringFromDate:createdAtDate];
        } else if (createdAtDate.bs_isToday) { // 今天
            
            NSDate *now = [NSDate date];
            //使用这个方法比较时间间隔--只需要判断时分秒
            NSCalendar *calendar = [NSCalendar bs_calendar];
            NSCalendarUnit unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
            NSDateComponents *cmps = [calendar components:unit fromDate:createdAtDate toDate:now options:0];
            
            if (cmps.hour >= 1) { // 时间间隔 >= 1小时
                return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
            } else if (cmps.minute >= 1) { // 1小时 > 时间间隔 >= 1分钟
                return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
            } else { // 时间间隔 < 1分钟
                return @"刚刚";
            }
          
        } else {
            fmt.dateFormat = @"MM-dd HH:mm:ss";
            return [fmt stringFromDate:createdAtDate];
        }
    } else { // 不是今年
        return _created_at;
    }    
    return _created_at;
}


/**
 *  对字体进行过滤
 */

-(NSString *)bs_setButtonTitle:(NSString *)numberStr placeholder:(NSString *)placeholder {
    
    NSInteger count = [numberStr integerValue];
   
    if (count >= 10000) {
        return [NSString stringWithFormat:@"%.1f万", count / 10000.0];
    }else if (count == 0){
        return placeholder;
    }else{
        return numberStr;
    }
}


-(NSString *)ding{

    _ding = [self bs_setButtonTitle:_ding placeholder:@"顶"];
    return _ding;
}
-(NSString *)cai{

    _cai = [self bs_setButtonTitle:_ding placeholder:@"踩"];
    return _cai;
}
-(NSString *)repost{
    _repost = [self bs_setButtonTitle:_ding placeholder:@"转发"];
    return _repost;
}

-(NSString *)comment{
    _comment = [self bs_setButtonTitle:_comment placeholder:@"评论"];
    return _comment;
}


-(CGFloat)cellHeight{
    //如果有值,就不进来
   if (_cellHeight) return _cellHeight;
    
    _cellHeight += 45 + BSMargin;
    //中间正文高度

    CGFloat textMaxW = BSScreenW - 2 * BSMargin;
    //    cellHeight += [topic.text sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(textMaxW, MAXFLOAT)].height + BSMargin;
    // options要传递NSStringDrawingUsesLineFragmentOrigin
    _cellHeight += [self.text boundingRectWithSize:CGSizeMake(textMaxW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height + BSMargin;
    
    //计算中间图片高度
    
    if (self.type !=  BSTopicTypeWord) {
        
        // 中间内容(比如图片)显示出来的宽度
        CGFloat centerW = textMaxW;
        // 中间内容(比如图片)显示出来的高度
        CGFloat centerH = self.height * centerW / self.width;
        
        if (centerH >= 300) { // 中间内容 >= 一个屏幕
            centerH = 200;
            self.BigPicture = YES;
        }
        
        self.centerF = CGRectMake(BSMargin, _cellHeight, centerW, centerH);
        _cellHeight += centerH + BSMargin;
        
    }
    
    //判读是否有最热评论,没有就返回
    //选取第一个作为最热评论
    if (self.top_cmt.count){
        //下面评论高度
        // 标题
        _cellHeight += 20;
        // 评论文字
        NSDictionary *cmt = self.top_cmt.firstObject;
        NSString *username = cmt[@"user"][@"username"];
        NSString *content = cmt[@"content"];
        if (content.length == 0) { // 语音评论
            content = @"[语音评论]";
        }
        NSString *cmtText = [NSString stringWithFormat:@"%@ : %@", username, content];
        //        cellHeight += [cmtText sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(textMaxW, MAXFLOAT)].height + BSMargin;
        _cellHeight += [cmtText boundingRectWithSize:CGSizeMake(textMaxW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height + BSMargin;
    }
    
       //底部高度
    _cellHeight += 35;
    
    //再加我们再 setFrame 减去的10
    _cellHeight += BSMargin;
    
    return _cellHeight;
}

@end
